<?php

/*聊天机器人*/

class Talk
{
    public static function chat($keywords)
    {
        $keywords = trim($keywords);
        $url = 'http://i.itpk.cn/api.php';
        $question = $keywords;
        $limit = 5;
        $api_key = '541bcde8258db5f275d12ed7a29e8c8f';
        $api_secret = 'ndf9d3cum2kx';
        $curl = new Curl\Curl();
        $getData = $curl->get($url, array(
            'question' => $question,
            'limit' => $limit,
            'api_key' => $api_key,
            'api_secret' => $api_secret
        ));

        /*判断聊天信息 只有这四种会返回json格式*/
        $array = ['笑话', '观音灵签', '月老灵签', '财神爷灵签'];
        if (!in_array($keywords, $array)) {
            return $getData->response;
        }
        $result ='';
        $data= json_decode(trim($getData->response,chr(239).chr(187).chr(191)),true);
        switch ($keywords) {
            case '笑话':
                $result = <<<TPL
标题:{$data['title']}
内容:{$data['content']}
TPL;
                break;
            case '观音灵签':
                $result = <<<TPL
灵签类型:{$data['type']}
签号:{$data['number1']}
此签:{$data['haohua']}
签语:{$data['qianyu']}
诗意解签:{$data['shiyi']}
白话解签:{$data['jieqian']}
TPL;
                break;
            case '月老灵签':
                $result = <<<TPL
灵签类型:{$data['type']}
签号:{$data['number1']}
此签:{$data['haohua']}
诗意解签:{$data['shiyi']}
白话解签:{$data['jieqian']}
注释:{$data['zhushi']}
白话解签:{$data['jieqian']}
TPL;
                break;
            case '财神爷灵签':
                $result = <<<TPL
灵签类型:{$data['type']}
签号:{$data['number1']}
签语:{$data['qianyu']}
注释:{$data['zhushi']}
白话解签:{$data['jieqian']}
解说:{$data['jieshuo']}
结果:{$data['jieguo']}
TPL;
                break;
        }
        return $result;
    }
}